<template >
  <div>
    <el-card>
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="方向名称">
      <el-input v-model="formInline.name" placeholder="方向名称"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">查询</el-button>
      <el-button type="primary" @click="toAdd">添加</el-button>
    </el-form-item>
  </el-form>
  <el-table
    :data="tableData"
    style="width: 100%">
    <el-table-column
      prop="id"
      label="id"
      width="180">
    </el-table-column>
    <el-table-column
      prop="name"
      label="方向名称"
      width="180">
    </el-table-column>

    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
        <el-button type="text" @click="deleteCollege(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="formInline.page"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="formInline.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <el-dialog title="修改" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="方向名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateCollege">确 定</el-button>
        </div>
      </el-dialog>


      <el-dialog title="添加" :visible.sync="dialogFormVisible1">
        <el-form :model="form">
          <el-form-item label="方向名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add1">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>


  </div>
</template>

<script>
  export default {
    data() {
      return {
       tableData:[],
        formInline: {
          name: '',
          page: 1,
          size:1
        },
        total:10,
        dialogFormVisible: false,
        dialogFormVisible1:false,
        formLabelWidth: '120px',
        form:{
         id:0,
          name:''
        }
      }
    },
    created(){
     this.showList();
    },
    methods:{
      add1(){
        this.$axios.post("college/save", this.form).then(res => {
          console.log(res);
          if (res.data.code == 201) {
            this.$message.error(res.data.msg)
          } else {
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.showList();
          }
        })
        this.dialogFormVisible1=false
      },
      deleteCollege(id){
        this.$axios.get('college/delete?id='+id).then((res) => {
          this.$message({
            message: '恭喜你，删除成功',
            type: 'success'
          });
          this.showList();
        })
      },
      updateCollege(){
        this.$axios.post('college/update',this.form).then((res) => {
          console.log(res)
          this.$message({
            message: '恭喜你，修改成功',
            type: 'success'
          });
          this.dialogFormVisible=false
          this.showList();
        })
      },
      handleClick(row) {
        console.log(row);
        this.dialogFormVisible=true;
        this.form.id=row.id;
        this.form.name=row.name
      },
      toAdd(){
        this.dialogFormVisible1=true;

      },
      showList(){
        this.$axios.post('college/searchpage',this.formInline).then((res) => {
          // console.log(res.data.data)
          this.tableData=res.data.data.records
          this.total=res.data.data.total
        })
      },
      onSubmit() {
        this.formInline.page=1
        this.showList();
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
       this.formInline.size= val
        this.showList()
      },
      handleCurrentChange(val) {
this.formInline.page=val
        this.showList()
      }
    }
  }
</script>

